package org.hyson.gofun;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

import android.graphics.Canvas;


import android.view.View;

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import android.content.res.Resources;

import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.util.Log;

public class GameView extends View{


	private int column;
	private int line;

	private int mOffsetX ;
	private int mOffsetY ;
	private Bitmap mPiecesArray[];
	private Game game;
	private Point touch_point; 

	private float move_x; 
	private float move_y;
	private boolean draw_move; 
	private boolean valid_key_down; 

	private final Paint mPaint = new Paint();
	public GameView(Context context) 
	{
		super(context);
		game = (Game)context;
		touch_point = new Point();
	}
	public GameView(Context context, int line, int column) 
	{
		super(context);
		game = (Game)context;
		touch_point = new Point();
		this.column  = column;
		this.line = line;
	}
	public void resetTiles(int tilecount) {
		mPiecesArray = new Bitmap[tilecount];
	}
	public void loadTile(int key, Drawable tile) {
		Bitmap bitmap = Bitmap.createBitmap(mOffsetX, mOffsetY, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, mOffsetX, mOffsetY);
		tile.draw(canvas);
		mPiecesArray[key] = bitmap;
	}
	private void initGameView() {
		setFocusable(true);
		setFocusableInTouchMode(true);

		Resources r = this.getContext().getResources();
		resetTiles(6);
		loadTile(1, r.getDrawable(R.drawable.black_pieces));
		loadTile(2, r.getDrawable(R.drawable.white_pieces));
		loadTile(3, r.getDrawable(R.drawable.empty_pieces));
		loadTile(4, r.getDrawable(R.drawable.forbid_pieces));
		loadTile(5, r.getDrawable(R.drawable.cur_pieces));
	}
	@Override
		protected void onSizeChanged(int w, int h, int oldw, int oldh) {
			mOffsetX = (w/(column));
			mOffsetY = (h/(line));
			initGameView();
		}
	protected int getBitmapIndex(int x, int y){
		int bitmap_index = 0;
		if(game.getBoard().getPos(x,y) == Pieces.P1){
			bitmap_index = 1;
		}
		if(game.getBoard().getPos(x,y) == Pieces.P2){
			bitmap_index = 2;
		}
		if(game.getBoard().getPos(x,y) == Pieces.EMPTY){
			bitmap_index = 3;
		}
		if(game.getBoard().getPos(x,y) == Pieces.FORBID){
			bitmap_index = 4;
		}
		return bitmap_index;
	}
	protected void onDraw(Canvas canvas){
		int bitmap_index = 0;
		super.onDraw(canvas);
		for(int i = 0; i < line; i++){
			for(int j = 0; j < column; j++){
				canvas.drawBitmap(mPiecesArray[3],(i)*mOffsetX,
						(j)*mOffsetY,
						mPaint 
						);
			}
		}
		for(int i = 0; i < line; i++){
			for(int j = 0; j < column; j++){
				if(game.getBoard().getPos(i,j) == Pieces.P1){
					bitmap_index = 1;
				}
				if(game.getBoard().getPos(i,j) == Pieces.P2){
					bitmap_index = 2;
				}
				if(game.getBoard().getPos(i,j) == Pieces.EMPTY){
					bitmap_index = 3;
				}
				if(game.getBoard().getPos(i,j) == Pieces.FORBID){
					bitmap_index = 4;
				}
				canvas.drawBitmap(mPiecesArray[bitmap_index],(i)*mOffsetX,
						(j)*mOffsetY,
						mPaint 
						);
			}
		}
		Point cur_pos = game.getBoard().getCurPoint();
		if(cur_pos.x >= 0  && cur_pos.y >=0){
			canvas.drawBitmap(mPiecesArray[5],cur_pos.x*mOffsetX,
					cur_pos.y*mOffsetY,
					mPaint 
					);
		}
		if(game.getCurPlayer().getWho() == Pieces.P1){
			bitmap_index = 1;
		}
		if(game.getCurPlayer().getWho()== Pieces.P2){
			bitmap_index = 2;
		}
		if(draw_move){
			canvas.drawBitmap(mPiecesArray[bitmap_index], move_x - mOffsetX/2, move_y - mOffsetY/2,
					mPaint 
					);
		}

	}
	@Override
		public boolean onTouchEvent(MotionEvent event) {
			int eventaction = event.getAction();

			float X = event.getX();
			float Y = event.getY();

			if(!game.getCurPlayer().getIsHuman()){
				super.onTouchEvent(event);
				return true;
			}
			switch (eventaction ) {
				case MotionEvent.ACTION_DOWN:
					valid_key_down = false;
					draw_move = false;
					if(check_pieces(X, Y, touch_point)){
						switch(game.getStatus()){
							case GAME_SET:
								game.getCurPlayer().setNextSet(touch_point);
								game.getCurPlayer().setThink();
								break;
							case GAME_KILL:
								game.getCurPlayer().setNextKill(touch_point);
								game.getCurPlayer().setThink();
								break;
							case GAME_MOVE:
								if(game.getBoard().getPos(touch_point.x, touch_point.y) == game.getCurPlayer().getWho()){
									game.getBoard().setPos(touch_point.x, touch_point.y,Pieces.EMPTY);
									game.getCurPlayer().setNextSrc(touch_point);
									valid_key_down = true;
								}
								break;
						}
					}
					break;
				case MotionEvent.ACTION_MOVE:
					if(valid_key_down){
						draw_move = true;
						move_x = X;
						move_y = Y;
						Log.d("Game","move_x = " + move_x + " move_y  = " + move_y);
					}
					break;
				case MotionEvent.ACTION_UP:
					{
						if(draw_move){
							draw_move = false;
							Log.d("Game", "draw up UP");
							if(game.getStatus() == GameStatus.GAME_MOVE){
								if(!check_pieces(X, Y, touch_point)){
									touch_point.x = -1;
									touch_point.y = -1;
								}
								game.getCurPlayer().setNextDst(touch_point);
								game.getCurPlayer().setThink();
							}
							break;
						}
					}  
			}
			// redraw the canvas  
			invalidate();   
			return true;   

		}
	private boolean check_pieces(float x, float y, Point pos)
	{
		for(int i = 0; i < column; i++){
			for(int j = 0; j < line; j++){
				if((int)x > i*mOffsetX && (int)x - i*mOffsetX < mOffsetX && (int)y - j*mOffsetY < mOffsetY&&
						(int)y > j*mOffsetY){
					pos.x = i; pos.y = j;
					return true;
				}

			}
		}
		return false;
	}

}
